package com.example.module_home.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.lib_common.utils.DateUtils
import com.example.module_home.R
import com.example.module_home.databinding.ItemSimpleVideoBinding
import com.example.module_home.model.entity.SimpleVideoEntity
import kotlin.math.max

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/28
 */
class SimpleRecyclerviewAdapter(
    val subscribeListener:SubscibeOnClickListener,
    val sharedListener:SharedOnClickListener,
    val playOnClickListener:PlayOnClickListener,
    val onPreload:(() -> Unit)? = null, //预加载回调
    val preloadItemCount:Int = 0 //预加载偏移量
):RecyclerView.Adapter<SimpleRecyclerviewAdapter.ViewHolder>() {
    private var isPreloading = false //是否正在预加载
    private var scrollState  = RecyclerView.SCROLL_STATE_IDLE //列表滑动状态
    private val data:MutableList<SimpleVideoEntity> = mutableListOf()
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                //更新状态
                scrollState = newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }
    /**
     * 设置新数据源
     */
    fun setData(list:MutableList<SimpleVideoEntity>){
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()

    }

    /**
     * 追加数据
     */
    fun addData(list: MutableList<SimpleVideoEntity>){
        val index = data.size
        data.addAll(list)
        notifyItemRangeInserted(index,list.size)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = ItemSimpleVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
       return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.initView(data[position])
        holder.initListener(data[position])
        checkPreload(position)
    }

    /**
     * 是否滑动到预加载位置/当前处于滑动中/不是正在预加载
     */
    private fun checkPreload(position: Int){
        if(onPreload != null
            && scrollState != RecyclerView.SCROLL_STATE_IDLE
            && position == max(data.size - 1 - preloadItemCount,0)
            && !isPreloading
        ){
            isPreloading = true
            onPreload.invoke()
            isPreloading = false

        }
    }



    //绑定数据+事件监听
    inner class ViewHolder(val binding:ItemSimpleVideoBinding):RecyclerView.ViewHolder(binding.root){
        val context = binding.root.context
        val headerImg = binding.ivItemSimpleInfoHeader
        val author = binding.tvItemSimpleInfoAuthor
        val publishTime = binding.tvItemSimpleInfoPublishtime
        val primaryImg = binding.ivItemSimpleInfoPrimaryimage
        val title = binding.tvItemSimpleInfoTitle
        val shared = binding.llItemSimpleInfoShared
        val commentNum = binding.llItemSimpleInfoCommentnum
        val ivagreeNum = binding.ivItemSimpleInfoAgree
        val agreeNum = binding.llItemSimpleInfoAgreenum
        val subscibe = binding.btnItemSimpleInfoSubscibe
        val txtCommentNum = binding.tvItemSimpleInfoCommentnum
        val txtAgreeNum = binding.tvItemSimpleInfoAgreenum
        val ivPlay=binding.ivItemSimpleInfoPlay
        /**
         * 绑定数据源
         */
        fun initView(entity: SimpleVideoEntity){

            Glide.with(context).load(entity.avatar_url).into(headerImg)
            author.text = entity.name
            publishTime.text = String.format(context.resources.getString(R.string.home_simple_videoinfo_publishtime), DateUtils.diffDateFromUTC(entity.ctime))
            Glide.with(context).load(entity.videomainimag).into(primaryImg)
            title.text = entity.title
            txtCommentNum.text = entity.commentnum.toString()
            txtAgreeNum.text = entity.playnum.toString()
        }
        fun initListener(entity: SimpleVideoEntity){

            ivPlay.setOnClickListener {
                if (null!=playOnClickListener){
                    playOnClickListener.onClick(entity)
                }

            }

            subscibe.setOnClickListener {
                if(null != subscribeListener){
                    subscribeListener.onClick(position,entity)
                }

            }

            ivagreeNum.setOnClickListener {
                if (ivagreeNum.tag==null || ivagreeNum.tag==false){
                    ivagreeNum.setImageDrawable(context.getDrawable(R.drawable.ic_thumb_up_check))
                    ivagreeNum.tag=true
                }
                else{
                    ivagreeNum.setImageDrawable(context.getDrawable(R.drawable.ic_thumb_up))
                    ivagreeNum.tag=false
                }

            }

            shared.setOnClickListener {
                sharedListener.onClick(position,entity)
            }
        }
    }


    interface PlayOnClickListener{
        fun onClick(entity:SimpleVideoEntity)
    }

    interface SubscibeOnClickListener{
        fun onClick(position:Int,entity:SimpleVideoEntity)
    }

    interface SharedOnClickListener{
        fun onClick(position:Int,entity:SimpleVideoEntity)
    }

}